<?php
class Model_agency extends CI_Model{
	protected $_table = "tbl_agecruise";
	protected $_cruises = "tbl_sheep";
	protected $_user = "tbl_user";
	public function __construct(){
		parent::__construct();
		$this->load->database();
	}
	public function listcrui(){
		$this->db->select("sheep_id,sheep_name");
		$this->db->order_by("sheep_name");
		return $this->db->get($this->_cruises)->result_array();
	}
	public function listall(){
		$this->db->where("status","1");
		$this->db->where("level","2");
		return $this->db->get($this->_user)->result_array();
	}
	public function getcruise($userid){
		$this->db->where("agency_id",$userid);
		$this->db->where("agecr_status","1");
		return $this->db->get($this->_table)->result_array();
	}
	public function add($agency_id,$cruises_id,$aname,$cname){
		$sql = "select * from ".$this->_table." where agency_id = '$agency_id' and cruises_id = '$cruises_id'";
		$rows = $this->db->query($sql)->num_rows();
		if($rows == 0){
			$sql = "insert into ".$this->_table."(agency_id,cruises_id,agency_name,cruises_name,agecr_time) values('$agency_id','$cruises_id','$aname','$cname','".date('d/m/Y-H:i:s')."')";
			$this->db->query($sql);
		}else{
			$sql = "update ".$this->_table." set agency_id='$agency_id',cruises_id='$cruises_id',agency_name='$aname',cruises_name='$cname' where agency_id='$agency_id' and cruises_id='$cruises_id'";
			$this->db->query($sql);
		}
	}
	public function delcruise($cruiseid,$where){
		$delete = implode(" and agency_id != ",$where);
		$sql = "delete from ".$this->_table." where agency_id != ".$delete." and cruises_id = '$cruiseid'";
		$this->db->query($sql);
	}
	public function del($id){
		$this->db->where("agecr_id",$id);
		$this->db->delete($this->_table);
	}
}
?>